﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1.SuYi.Week2WinForm
{
    public partial class WaitingWinFormShow : Form
    {
        public WaitingWinFormShow()
        {
            InitializeComponent();
           //GetWaiting();
            comboBox1.Items.Add("请选择");
            comboBox1.Items.Add("自营待宰圈");
            comboBox1.Items.Add("代宰待宰圈");
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private int PageIndex = 1;
        private int PageSize = 3;
        public string WaitingType = "";
        public string WaitingDuty = "";
        public  int totalPage=0 ;
         public int totalCount =0;

        private async void WaitingWinFormShow_Load(object sender, EventArgs e)
        {
            GetWaiting();
        }
        public async void GetWaiting()
        {

            var page = new WaitingQueryPage()
            {
                PageIndex = PageIndex,
                PageSize = PageSize,
                WaitingType = WaitingType,
                WaitingDuty = WaitingDuty,
            };

            var item = await HttpClientHelper.RequestUrl<object, ApiResultFY2<List<WaitingsDto>>>(URLCommon.Url2, "/Week2Get/QueryWaiting", HttpClientHelper.MethodType.POST, page);

            this.dataGridView1.DataSource = item.Data.list;
            totalCount = item.Data.totalCount;
            totalPage = item.Data.totalPage;
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class WaitingQueryPage
        {

            /// <summary>
            /// 页码
            /// </summary>
            public int PageIndex { get; set; }
            /// <summary>
            /// 条数
            /// </summary>
            public int PageSize { get; set; }
            /// <summary>
            /// 待宰圈类型
            /// </summary>
            public string WaitingType { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public string WaitingDuty { get; set; }

        };
        /// <summary>
        /// 显示字段
        /// </summary>
        public class WaitingsDto
        {
            /// <summary>
            /// 待宰圈编号
            /// </summary>
            public int WaitingId { get; set; }
            /// <summary>
            /// 待宰圈名称
            /// </summary>
            public string WaitingName { get; set; }
            /// <summary>
            /// 待宰圈类型
            /// </summary>
            public string WaitingType { get; set; }
            /// <summary>
            /// 顺序号
            /// </summary>
            public string WaitingNo { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool WaitingStatu { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string WaitingRemark { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public string WaitingDuty { get; set; }
            /// <summary>
            /// 部门名称
            /// </summary>
            public string SectionName { get; set; }
            /// <summary>
            /// 逻辑删除
            /// </summary>
            public int IsDel { get; set; }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            WaitingDuty = textBox1.Text;
            WaitingType = comboBox1.Text;
            GetWaiting();
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {

        }
        private async void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["删除"].Index)
            {
                var waitdel = (int)e.RowIndex;
                if (waitdel > 0)
                {
                    int WaitingId = Convert.ToInt32(dataGridView1.Rows[waitdel].Cells["待宰圈编号"].Value);

                    DeleteIds deleteId =new DeleteIds()
                    {
                        WaitingId= WaitingId
                    };

                    var item = await HttpClientHelper.RequestUrl<DeleteIds, ApiResult<List<DeleteIds>>>(URLCommon.Url1, "/Week2Setup/DeleteWaiting", HttpClientHelper.MethodType.POST, deleteId);



                    if (item.Code==1)
                    {
                        MessageBox.Show("删除成功");
                        GetWaiting();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                }

              
            }
        }
        /// <summary>
        /// 状态显示判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["状态"].Index && e.Value != null)
            {
                if (e.Value.ToString() == "True")
                {
                    e.Value = "启用";
                    e.FormattingApplied = true;
                }
                else
                {
                    e.Value = "禁用";
                    e.FormattingApplied = false;
                }
            }
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex--;
                
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            GetWaiting();
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            GetWaiting();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (PageIndex < totalPage)
            {
                PageIndex++;
                
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            GetWaiting();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            PageIndex = totalPage;
            GetWaiting();
        }

        /// <summary>
        /// 编号
        /// </summary>
        public class DeleteIds
        {
            public int WaitingId {  get; set; }
        }
    }
}
